package club.qwertyzzz;

import lombok.Getter;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;

public class TankFrame extends Frame {

    Tank mainTank = new Tank(200, 200, Dir.DOWN, false, TankType.player1 ,this);

    @Getter
    private java.util.List<Bullet> bullets = new ArrayList<>();

    @Getter
    private java.util.List<Tank> enemyTanks = new ArrayList<>();

    @Getter
    private java.util.List<Blast> blasts = new ArrayList<>();

    static final int GAME_HEIGHT = 600, GAME_WIDTH = 800;
    private Image offScreenImage = null;

    public TankFrame() throws HeadlessException {
        // 设置大小
        this.setSize(GAME_WIDTH, GAME_HEIGHT);

        // 设置能否改变大小
        this.setResizable(false);

        // 设置标题
        this.setTitle("tank war");

        // 设置可见
        this.setVisible(true);

        // 添加窗口的监听器，设置窗口一些事件的响应动作
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosed(e);
                System.exit(0);
            }
        });

        // 添加键盘事件
        this.addKeyListener(new MyKeyListener());

        // 播放开始音乐
        ResourceMgr.playStartSound();
    }

    @Getter
    private Random gameRandom = new Random();

    @Override
    public void update(Graphics g) {
        // 创建一个Image
        if(offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }

        // 拿到基于此Image的画笔
        Graphics goffScreen = offScreenImage.getGraphics();
        Color c = goffScreen.getColor();
        goffScreen.setColor(Color.black);

        // 图像画成黑色
        goffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        goffScreen.setColor(c);

        // 将坦克、子弹等内容画到Image上
        paint(goffScreen);

        // 将图片画到窗口上
        g.drawImage(offScreenImage, 0, 0, null);
    }

    /**
     * 窗口第一次出来，改变大小，被覆盖重新显示的时候调用 => 窗口需要重新绘制的时候
     * @param g 系统传递过来的一支画笔
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        // 输出提示信息
        Color originalColor = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹的数量：" + bullets.size(), 10, 60);
        g.drawString("敌方坦克的数量：" + enemyTanks.size(), 10, 90);
        g.setColor(originalColor);

        // 画坦克
        mainTank.paint(g);

        // 画子弹
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }

        // 画敌方坦克
        for(int i = 0; i < enemyTanks.size(); i++) {
            enemyTanks.get(i).paint(g);
        }

        // 画完坦克检测碰撞
        for(int i = 0; i < bullets.size(); i++) {
            for(int j = 0; j < enemyTanks.size(); j++) {
                bullets.get(i).coordinateWith(enemyTanks.get(j));
                if(i >= bullets.size()) {
                    break;
                }
            }
        }

        // 画出爆炸效果
        for(int i = 0; i < blasts.size(); i++) {
            blasts.get(i).paint(g);
        }
    }

    class MyKeyListener extends KeyAdapter {

        boolean bR = false;
        boolean bL = false;
        boolean bU = false;
        boolean bD = false;

        /**
         * 键盘被按下去的时候调用
         * @param e
         */
        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;

                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;

                case KeyEvent.VK_UP:
                    bU = true;
                    break;
            }

            // 更改坦克的方向
            setMainTankDirection();
        }

        private void setMainTankDirection() {
            // 设置坦克状态为移动
            mainTank.setMoving(true);

            // 设置坦克方向
            if(bL) mainTank.setDir(Dir.LEFT);
            else if(bR) mainTank.setDir(Dir.RIGHT);
            else if(bU) mainTank.setDir(Dir.UP);
            else if(bD) mainTank.setDir(Dir.DOWN);

            // 如果所有方向键盘都释放了，则设置坦克为不移动
            if(!bL && !bR && !bD && !bU)
                mainTank.setMoving(false);
        }

        /**
         * 键被抬起来的时候调用
         * @param e
         */
        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;

                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;

                case KeyEvent.VK_UP:
                    bU = false;
                    break;

                case KeyEvent.VK_SPACE:
                    mainTank.fire();
                    break;
            }

            // 更改坦克的方向
            setMainTankDirection();
        }
    }
}
